﻿using System;
using System.Collections.Generic;
using Kooboo.CMS.Content.EventBus;
using Kooboo.CMS.Content.EventBus.Content;
using Kooboo.CMS.Content.Models;
using Kooboo.CMS.Sites.Models;
namespace KB.Subscribers
{
    public abstract class SubscriberRegistration : ISubscriber
    {
        public abstract string AssemblyName
        {
            get;
        }
        public abstract string SubscribersNamespace
        {
            get;
        }
        public static IDictionary<string, Subscriber> Subscribers = new Dictionary<string, Subscriber>();

        public void Receive(IEventContext context)
        {
            if (context is ContentEventContext)
            {
                var eventContext = (ContentEventContext)context;
                var content = eventContext.Content;
                if (content is TextContent)
                {
                    //XXXSubscriber
                    var key = string.Format("{0}.{1}Subscriber,{2}", SubscribersNamespace, content.SchemaName, AssemblyName);
                    if (!Subscribers.ContainsKey(key))
                    {
                        var Tsubscriber = Type.GetType(key);
                        if (Tsubscriber != null)
                        {
                            var subscriberDerived = Activator.CreateInstance(Tsubscriber);
                            if (!(subscriberDerived is Subscriber))
                            {
                                throw new InvalidCastException(content.SchemaName + "Subscriber must derive from KB.Subscribers.Subscriber");
                            }
                            Subscribers.Add(key, (Subscriber)subscriberDerived);
                        }
                        else
                        {
                            Subscribers.Add(key, null);
                            return;
                        }
                    }
                    var subscriber = Subscribers[key];
                    if (subscriber != null)
                    {
                        if (subscriber.SiteName != null && subscriber.SiteName != Site.Current.Name)
                        {
                            return;
                        }
                        switch (eventContext.ContentAction)
                        {
                            case ContentAction.PreAdd:
                                subscriber.PreAdd(content);
                                break;
                            case ContentAction.PreUpdate:
                                subscriber.PreUpdate(content);
                                break;
                            case ContentAction.PreDelete:
                                subscriber.PreDelete(content);
                                break;
                            case ContentAction.Add:
                                subscriber.Add(content);
                                break;
                            case ContentAction.Update:
                                subscriber.Update(content);
                                break;
                            case ContentAction.Delete:
                                subscriber.Delete(content);
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
    }
}
